package cn.com.w.common.DTO;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author w
 * @Description:
 * @DateTime: 2025 04 25 17:38
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SysUserDTO implements UserDetails {
    /**
     * 权限
     */
    private Long id;
    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 用户状态，1-开启-0禁用
     */
    private Boolean status;
    /**
     * 手机号
     */
    private String phoneNumber;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 微信
     */
    private String wechat;
    /**
     * 密码是否失效，1-可用，0-失效
     */
    private Boolean passwordNonExpired;
    /**
     * 用户关联的所有角色
     */
    private List<SysRoleDTO> roles = new ArrayList<>();

    //标记该字段不做json处理
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return passwordNonExpired != null && passwordNonExpired;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return status != null && status;
    }

}